#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mainWindowInit();
}

void MainWindow::mainWindowInit()
{
    this->setWindowTitle("阅读器");
    this->setGeometry(0,0,480,500);
    menu = new QMenuBar(this);
    menu->installEventFilter(this);
    menu->setStyleSheet("QMenu::item{ padding:20px;}");

    QFont font;
    font.setPixelSize(15);
    menu->setFont(font);

    fileMenu = new QMenu(this);
    helpMenu = new QMenu(this);
    readingMenu = new QMenu(this);

    fileMenu = menu->addMenu("   文件   ");
    helpMenu = menu->addMenu("   版本   ");
    readingMenu = menu->addMenu("   模式   ");
    supportMenu = menu->addMenu("   辅助   ");
    menu->setGeometry(0,0,this->geometry().width(),28);

    openAct = new QAction(QIcon(""),"打开",this);
    openAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
    openAct->setStatusTip("打开");
    quitAct = new QAction(QIcon(""),"退出",this);
    quitAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
    quitAct->setStatusTip("关闭");
    fileMenu->addAction(openAct);
    fileMenu->addAction(quitAct);

    aboutAct = new QAction(QIcon(""),"关于",this);
    aboutAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_H));
    aboutAct->setStatusTip("关于");
    helpMenu->addAction(aboutAct);

    normalModelAct = new QAction(QIcon(""),"经典模式",this);
    normalModelAct->setStatusTip("经典模式");
    greenEyeModelAct = new QAction(QIcon(""),"绿色护眼",this);
    greenEyeModelAct->setStatusTip("绿色护眼");
    nightModelAct = new QAction(QIcon(""),"夜间模式",this);
    nightModelAct->setStatusTip("夜间模式");
    pergamynModelAct = new QAction(QIcon(""),"羊皮纸",this);
    pergamynModelAct->setStatusTip("羊皮纸");
    readingMenu->addAction(normalModelAct);
    readingMenu->addAction(greenEyeModelAct);
    readingMenu->addAction(nightModelAct);
    readingMenu->addAction(pergamynModelAct);

    colorAct = new QAction(QIcon(""),"颜色",this);
    fontZoomInAct = new QAction(QIcon(""),"放大",this);
    fontZoomOutAct = new QAction(QIcon(""),"缩小",this);
    supportMenu ->addAction(colorAct);
    supportMenu ->addAction(fontZoomInAct);
    supportMenu ->addAction(fontZoomOutAct);

    textBrowser = new QTextBrowser(this);
    textInfo = new QLabel(this);

    //设置文本编辑区的大小与位置
    textBrowser->setFont(QFont("宋体", 15));
    textBrowser->setGeometry(0,28,this->geometry().width(),this->geometry().height());
    //禁止鼠标交互
    textBrowser->setTextInteractionFlags ( Qt::NoTextInteraction );
    textInfo->setGeometry(10,30,80,20);


    connect(aboutAct,SIGNAL(triggered()),this,SLOT(slotactAbout()));
    connect(openAct,SIGNAL(triggered()),this,SLOT(slotOpen()));
    connect(quitAct,SIGNAL(triggered()),this,SLOT(slotQuit()));
    connect(normalModelAct,SIGNAL(triggered()),this,SLOT(slotactNormalReading()));
    connect(greenEyeModelAct,SIGNAL(triggered()),this,SLOT(slotactgreenEyeReading()));
    connect(nightModelAct,SIGNAL(triggered()),this,SLOT(slotactnightReading()));
    connect(pergamynModelAct,SIGNAL(triggered()),this,SLOT(slotactpergamynReading()));
    connect(fontZoomInAct,SIGNAL(triggered()),this,SLOT(slotFontZoomIn()));
    connect(fontZoomOutAct,SIGNAL(triggered()),this,SLOT(slotFontZoomOut()));
    connect(colorAct,SIGNAL(triggered()),this,SLOT(slotGetcolor()));
}
void MainWindow::slotOpen()
{
    QString fileName;
    fileName = QFileDialog::getOpenFileName(this, "选择文件", "", "*.txt *.sh *.py *.cpp *.c", 0, QFileDialog::DontUseNativeDialog|QFileDialog::ReadOnly);
       if(!fileName.isEmpty()){
           loadFile(fileName);
       }
}
/* 加载文件的函数 */
void MainWindow::loadFile(QString fileName)
{
    QFile file(fileName);
    if(!file.open(QFile::ReadOnly | QFile::Text)){
        QMessageBox::critical(this,"critical","cannot read file");
    }else{
        QTextStream in(&file);
        textBrowser->setText(in.readAll());
    }
}

void MainWindow::slotQuit()
{
    qApp->quit();
}

void MainWindow::slotactAbout()
{
    QMessageBox box;
    box.about(this,"Copyright","@EVAN_XIA");
}

void MainWindow::slotactNormalReading()
{
    textBrowser->setStyleSheet("background-color: rgba(245, 245, 245, 0.8)");
}

void MainWindow::slotactgreenEyeReading()
{
    textBrowser->setStyleSheet("background-color: rgba(84, 255, 159, 0.8)");
}

void MainWindow::slotactnightReading()
{
    textBrowser->setStyleSheet("background-color: rgba(79, 79, 79, 0.8)");
}

void MainWindow::slotactpergamynReading()
{
    textBrowser->setStyleSheet("background-color: rgba(238, 118, 0, 0.8)");
}
void MainWindow::slotFontZoomIn()
{
    textBrowser->zoomIn(1);
}

void MainWindow::slotFontZoomOut()
{
    textBrowser->zoomOut(1);
}
void MainWindow::slotGetcolor()
{
    QColor color = QColorDialog::getColor(Qt::red,this);
    if(color.isValid()){
        QTextCharFormat fmt;
        fmt.setForeground(color);
        QTextCursor textCursor = textBrowser->textCursor();//获取鼠标是否有选中文件
        if(textCursor.hasSelection()== false){
            textBrowser->selectAll();
        }
        textBrowser->mergeCurrentCharFormat(fmt);
        textCursor.clearSelection();
        textBrowser->setTextCursor(textCursor);
    }
}
MainWindow::~MainWindow()
{
    delete ui;
}
